/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package traitement_xml;

import java.io.File;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author phoebus
 */
public class lecture_machine {

    /**
     * retour d une liste contenant la liste des machine c'est une ArrayList qui
     * est retourné
     *
     * @return une liste de STRING
     */
    public static ArrayList<String> list_machine() {

        ArrayList<String> maliste = new ArrayList<>();
        try {
            File fXmlFile = new File("machines.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();

            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
            Element racine = doc.getDocumentElement();

            //Node la_machine = (Node) racine.getElementsByTagName("MACHINES");

            System.out.println(racine.getChildNodes().getLength());
            for (int i = 0; i < racine.getChildNodes().getLength(); i++) {
                if (racine.getChildNodes().item(i).getNodeName() != "#text") {
                    System.out.println(racine.getChildNodes().item(i).getNodeName());
                    maliste.add(racine.getChildNodes().item(i).getNodeName());
                }

            }

        } catch (Exception e) {
            System.out.println("erreur");
            System.out.println(e.getMessage());
        }

        return maliste;
    }
}
